package com.faradel.pharmapp.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.lang.StringUtils;
import org.faradel.pharmapp.entities.OrderMedication;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.faradel.pharmapp.service.OrderMedicationServiceInterface;

/**
 * This bean person inventory order creation, deletion and updating
 * 
 * @author michaeledegbe
 * 
 */
@ManagedBean
@ViewScoped
public class OrderInventoryBean implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Resource
    private OrderMedication orderMedication;

    private String orderId;
    private String medicationId;

    private Map<String, String> searchMap;

    private String action;

    private List<OrderMedication> orderList;

    /**
     * Initialize
     */
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        orderMedication = (OrderMedication) context.getBean("orderMedication");

        searchMap = new HashMap<String, String>();
        setOrderList(new ArrayList<OrderMedication>());

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

        action = params.get("action");
    }

    private OrderMedicationServiceInterface getOrderMedicationServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (OrderMedicationServiceInterface) context.getBean("orderMedicationServiceImpl");
    }

    public void findOrderMedicationByMedicationId()
    {
        clearList();
        searchMap.clear();
        searchMap.put("medicationId", medicationId);
        searchMap.put("orderStatus", "Open");

        orderList = getOrderMedicationServiceInterface().getOrderMedicationByColumns(searchMap);
        clearMedicationIdBox();
    }

    public void findOrderMedicationByOrderId()
    {
        clearList();
        searchMap.clear();
        searchMap.put("orderId", orderId);
        searchMap.put("status", "Open");

        orderList = getOrderMedicationServiceInterface().getOrderMedicationByColumns(searchMap);
        clearOrderIdBox();
    }

    public void findAllOpenOrders()
    {
        clearList();
        searchMap.clear();
        searchMap.put("orderStatus", "Open");

        orderList = getOrderMedicationServiceInterface().getOrderMedicationByColumns(searchMap);
        clearMedicationIdBox();
        clearOrderIdBox();
    }

    private void clearList()
    {
        if (orderList.size() > 0)
            orderList.clear();
    }

    private void clearMedicationIdBox()
    {
        if (StringUtils.isNotEmpty(medicationId))
            medicationId = null;
    }

    private void clearOrderIdBox()
    {

        if (StringUtils.isNotEmpty(orderId))
            medicationId = null;
    }

    public String update()
    {
        action = "update";
        return "processinventoryorder";
    }

    public String delete()
    {
        action = "delete";
        return "processinventoryorder";
    }

    public String view()
    {
        action = "view";
        return "processinventoryorder";
    }

    public OrderMedication getOrderMedication()
    {
        return orderMedication;
    }

    public void setOrderMedication(OrderMedication orderMedication)
    {
        this.orderMedication = orderMedication;
    }

    public String getOrderId()
    {
        return orderId;
    }

    public void setOrderId(String orderId)
    {
        this.orderId = orderId;
    }

    public Map<String, String> getSearchMap()
    {
        return searchMap;
    }

    public void setSearchMap(Map<String, String> searchMap)
    {
        this.searchMap = searchMap;
    }

    public String getAction()
    {
        return action;
    }

    public void setAction(String action)
    {
        this.action = action;
    }

    public String getMedicationId()
    {
        return medicationId;
    }

    public void setMedicationId(String medicationId)
    {
        this.medicationId = medicationId;
    }

    public List<OrderMedication> getOrderList()
    {
        return orderList;
    }

    public void setOrderList(List<OrderMedication> orderList)
    {
        this.orderList = orderList;
    }

}
